package com.jindy.module.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.DateUtils;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.product.domain.Product;
import com.jindy.module.product.domain.ProductModel;
import com.jindy.module.product.mapper.ProductMapper;
import com.jindy.module.product.mapper.ProductModelMapper;
import com.jindy.module.product.service.IProductService;
import com.jindy.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 产品Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class ProductServiceImpl implements IProductService
{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ProductModelMapper productModelMapper;
    /**
     * 查询产品
     *
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public Product selectProductById(Long id)
    {
        return productMapper.selectProductById(id);
    }

    /**
     * 查询产品列表
     *
     * @param product 产品
     * @return 产品
     */
    @Override
    public List<Product> selectProductQuantityList(Product product)
    {
        return productMapper.selectProductQuantityList(product);
    }

    /**
     * 查询产品列表
     *
     * @param product 产品
     * @return 产品
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增产品
     *
     * @param product 产品
     * @return 结果
     */
    @Override
    public int insertProduct(Product product)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        product.setCreateId(sysUser.getUserId().toString());
        product.setCreateBy(sysUser.getUserName());
        product.setCreateTime(DateUtils.getNowDate());
        product.setPutOnTime(DateUtils.getNowDate());
        int result = productMapper.insertProduct(product);
        //添加型号
        if(!CollectionUtils.isEmpty(product.getModels())){
            product.getModels().forEach(productModel ->{
                productModel.setProductId(product.getId().toString());
                productModel.setProductNo(product.getProductNo());
                productModel.setProductName(product.getProductName());
            } );
            productModelMapper.insertProductModels(product.getModels());
        }
        return result;
    }

    /**
     * 修改产品
     *
     * @param product 产品
     * @return 结果
     */
    @Override
    public int updateProduct(Product product)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        product.setUpdateBy(sysUser.getUserName());
        product.setUpdateTime(DateUtils.getNowDate());
        //修改型号
        if(!CollectionUtils.isEmpty(product.getModels())){
            ProductModel model = new ProductModel();
            model.setProductId(product.getId().toString());
            //修改产品前的型号列表
            List<ProductModel> oldList = productModelMapper.selectProductModelList(model);
            List<Long> delId = new ArrayList<>();
            List<ProductModel> upList = new ArrayList<>();
            List<ProductModel> newList = new ArrayList<>();

            for(ProductModel m:product.getModels()){
                if(m.getId() == null){
                    newList.add(m);
                }else{
                    upList.add(m);
                }
            }

            if(upList.size()>0){
                for(ProductModel old:oldList) {
                    boolean isDel = true;
                    for(ProductModel m:upList){
                        if(old.getId().equals(m.getId())){
                            isDel = false;
                            break;
                        }
                    }
                    if(isDel){
                        delId.add(old.getId());
                    }
                }
                //修改型号
                productModelMapper.updateProductModels(upList);
                if(delId.size()>0){
                    //删除型号
                    productModelMapper.deleteProductModelByIds(delId);
                }
            }
            if(newList.size()>0){
                newList.forEach(productModel ->{
                    productModel.setProductId(product.getId().toString());
                    productModel.setProductNo(product.getProductNo());
                    productModel.setProductName(product.getProductName());
                } );
                productModelMapper.insertProductModels(newList);
            }


        }
        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除产品
     *
     * @param ids 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(Long[] ids)
    {
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 删除产品信息
     *
     * @param id 产品主键
     * @return 结果
     */
    @Override
    public int deleteProductById(Long id)
    {
        return productMapper.deleteProductById(id);
    }

    public static void main(String[] args) {
        Long a=1L;
        Long b=1L;
        System.out.println(a.equals(b));
    }
}
